﻿using MessagePack;
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace DiverseStudents.Mods
{
    [MessagePackObject]
    public struct ModArchive
    {
        [Key(0)]
        public string id;
        [Key(1)]
        public string name;
        [Key(2)]
        public string author;
        [Key(3)]
        public string version;
        [Key(4)]
        public string description;
        [Key(5)]
        public bool hasImage;//是否有缩略图
        [Key(6)]
        public bool check;//是否启用
        [Key(7)]
        private byte[] hash;
        [Key(8)]
        public List<string> tags;
        [Key(9)]
        public List<ValueTuple<string, List<string>>> res;//Reource 文件目录
        [Key(10)]
        public List<ValueTuple<string, List<string>>> cfgs;//Cfg 文件目录

        public bool needFresh;

        //public uint modIndex;
        public void Refresh()
        {
            CalculateHash(Path.Combine( DConfig.loadPath, id));
        }


        private void CalculateHash(string path)
        {
            if (res == null && Directory.Exists(path))
            {
                var cfgPath = path + "/Cfgs";
                if (GetSubFilePath(cfgPath, out byte[] cfgHash, out cfgs)) { }
                var resPath = path + "/Resoures";
                if (GetSubFilePath(resPath, out byte[] resHash, out res))
                {
                    if (cfgHash.Length > 0)
                    {
                        var max = Math.Max(cfgHash.Length, resHash.Length);
                        for (int i = 0; i < max; i++)
                        {
                            cfgHash[i] ^= resHash[i];
                        }
                    }
                }
                hash = cfgHash;
            }
        }


        private bool GetSubFilePath(string path, out byte[] hashResult, out List<(string, List<string>)> lists)
        {
            lists = new List<(string, List<string>)> { };
            hashResult = new byte[0];
            if (Directory.Exists(path))
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(path);
                MD5 temperhash = MD5.Create();
                foreach (FileInfo file in directoryInfo.GetFiles())
                {
                    DirectoryInfo subDirectoryInfo = new DirectoryInfo(file.FullName);
                    var subfiles = subDirectoryInfo.GetFiles();
                    if (subfiles.Length == 0)
                        continue;
                    var tempList = new List<string>();
                    string sumString = string.Empty;
                    foreach (FileInfo fileInfo in subfiles)
                    {
                        sumString += fileInfo.Name + fileInfo.LastWriteTime;
                        tempList.Add(fileInfo.Name);
                    }
                    hashResult = temperhash.ComputeHash(Encoding.UTF8.GetBytes(sumString));
                    lists.Add((file.Name, tempList));
                }

                return true;
            }
            return false;
        }
   
    
    }
}
